package com.gin.supermarket_inventory_system.inventory.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gin.database.service.MyService;
import com.gin.supermarket_inventory_system.inventory.entity.InventoryReceiptDetailPo;
import com.gin.supermarket_inventory_system.inventory.enums.InventoryReceiptType;

import java.util.Collection;
import java.util.List;

/**
 *
 * @since : 2023/10/11 11:44
 * @author : ginstone
 * @version : v1.0.0
 **/
public interface InventoryReceiptDetailService extends MyService<InventoryReceiptDetailPo> {

    /**
     * 根据单据ID 查询单据详情
     * @param receiptId 单据ID
     * @return 单据详情
     */
    default List<InventoryReceiptDetailPo> listByReceiptId(long receiptId) {
        final QueryWrapper<InventoryReceiptDetailPo> qw = new QueryWrapper<>();
        qw.eq("receipt_id", receiptId);
        return list(qw);
    }


    /**
     * @param start 时间区间，起
     * @param end 时间区间，止
     * @return 统计指定时间区间, 指定库存的销售情况（数量，总金额）
     */
    default List<InventoryReceiptDetailPo> countInventoryGroupByDate(long start, long end, Collection<Long> inventoryId) {
        final QueryWrapper<InventoryReceiptDetailPo> qw = new QueryWrapper<>();
        // 查询类型 日期 ，数量，总额
        qw.select(
                        "DATE_FORMAT(FROM_UNIXTIME(time_create), '%Y-%m-%d') as date"
                        , "count(1) as count"
                        , "sum(total_price) as total_price"
                )
                // 库存id
                .in("inventory_id", inventoryId)
                // 销售类型
                .eq("type", InventoryReceiptType.SELL)
                .ge("time_create", start)
                .le("time_create", end)
                .groupBy("date");
        return list(qw);
    }

    /**
     * @param start 时间区间，起
     * @param end 时间区间，止
     * @return 统计指定时间区间, 指定商品的销售情况（数量，总金额）
     */
    List<InventoryReceiptDetailPo> countCommodityGroupByDate(long start, long end, long commodityId);

    /**
     * 根据库存id和类型查询单据详情
     * @param type 类型
     * @param inventoryId 库存id
     * @return
     */
    default List<InventoryReceiptDetailPo> listByInventoryId(InventoryReceiptType type, Collection<Long> inventoryId) {
        final QueryWrapper<InventoryReceiptDetailPo> qw = new QueryWrapper<>();
        qw.eq("type", type.name());
        qw.in("inventory_id", inventoryId);
        return list(qw);
    }

    /**
     * 查询指定库存的单据详情数
     * @param inventoryId 库存id
     * @return 单据详情数量
     */
    default long countByInventoryId(long inventoryId) {
        final QueryWrapper<InventoryReceiptDetailPo> qw = new QueryWrapper<>();
        qw.eq("inventory_id", inventoryId);
        return count(qw);
    }
}